package servlet;

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

import javax.servlet.*;
import javax.servlet.http.*;

import common.MySearchElement;
import common.Property;
import common.SkillManInstance;
import common.SkillManSchema;
import common.SkillManSearch;

public class searchInstance extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String cwd=Property.skillMgmHome;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


		HttpSession session = request.getSession(true);
		
		SkillManSchema schema=new SkillManSchema();
		schema.setCWD(Property.skillMgmHome);
		schema.load();

		SkillManInstance base = new SkillManInstance(schema, " .owl");
		base.setCWD(Property.instances);
		base.loadDefault(Property.skillMgmHome+"default_instance.owl");
		base.setPropertyValue(SkillManInstance.foafNS+"Person", SkillManInstance.foafNS+"name" ,"Jocopoco Majoco");
		
		Enumeration<String> e=request.getParameterNames();
		int numParent=0;
		while (e.hasMoreElements())
		{
			String c=e.nextElement();
			if(c.contains("parent"))
			{
				numParent++;
			}
		}
		//System.out.println(numParent);
		for(int j=0;j<numParent;j++){
			String skill=request.getParameter("parent"+j);
			String leve=request.getParameter("level"+j);
			int livello=Integer.parseInt(leve);
			base.addSkill(skill, livello);
			System.out.println("Inserted skill: "+skill);
			System.out.println("With level: "+livello);
		}
		
		System.out.println("Name: "+base.getProperyValue(SkillManInstance.foafNS+"Person", SkillManInstance.foafNS+"name"));
		System.out.println("Java skill level: "+base.getSkillLevel("Java"));
		
		SkillManSearch search=new SkillManSearch(schema, base, Property.instances );
		search.loadFiles();
		search.loadOnts();
		search.compareToAll();
		
		System.out.println("Vettori");
		Vector<MySearchElement> risultati=search.getResults();
		session.setAttribute("results", risultati);	
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("showResults.jsp");
		dispatcher.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
